﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jzy.Model.Project;

using Jzy.DAL.Project;
using System.Web.UI.WebControls;
using System.Data;

using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.BLL.Project
{
    public class ProjectBO:CommonOperate<ProjectInfo>
    {
        private ProjectDAL _prj;
        protected override BaseDAL DAL
        {
            get
            {
                return _prj;
            }
        }
        public ProjectBO()
            : base("Project")
        {
            _prj = new ProjectDAL();
        }

        public void BindYear(DropDownList drp,params PrjStatus[] status)
        {
            string tmp = drp.SelectedValue;
            drp.Items.Clear();            
            int minYear;
            int maxYear;

            _prj.SetYear(out minYear, out maxYear, status);

            string selectYear = maxYear.ToString();
            for (int i = minYear; i <= maxYear; i++)
            {
                drp.Items.Add(new ListItem(i.ToString(), i.ToString()));
                if (tmp == i.ToString())
                {
                    selectYear = tmp;
                }
                else
                {
                    if (i == DateTime.Now.Year)
                    {
                        selectYear = i.ToString();
                    }
                }
            }

            drp.SelectedValue = selectYear;
            
        }


        public DataTable GetDataTable(int status)
        {
            DataTable dt = _prj.GetDataTable(status);
            return dt;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="drp"></param>
        /// <param name="Status">判断是否来自于归档项目或作废项目</param>
        /// <param name="userID">判断是否为个人项目或全部项目</param>
        public void BindPrj(DropDownList drp,int? Status, int? userID)
        {
            DataTable dt = null;
            if (Status != null)
            {
                dt = _prj.GetDataTable((int)Status);
            }
            else
            {
                dt = _prj.GetDataTable(null, -1, userID);
            }
            drp.DataSource = dt;
            drp.DataTextField = "Name";
            drp.DataValueField = "ID";
            drp.DataBind();
        }

        public System.Data.DataTable GetDataTable(string year, int status)
        {
            DataTable dt = this.GetDataTable(year, status, null);
            return dt;
        }

        public System.Data.DataTable GetDataTable(string year, int status, int? userID)
        {
            DataTable dt = _prj.GetDataTable(year, status, userID);

            dt.Columns.Add("ScheduleRemark");
            dt.Columns.Add("ScheduleUserName");
            dt.Columns.Add("ScheduleInputTime",typeof(DateTime));

            dt.Columns.Add("OverseeRemark");
            dt.Columns.Add("OverseeUserName");
            dt.Columns.Add("OverseeInputTime",typeof(DateTime));

            if (dt.Rows.Count > 0)
            {
                List<int> prjIDs = ConvertUtil.ConvertToList(dt);

                DataTable tmp1 = TaskScheduleDAL.GetLastRemark(prjIDs);

                DataTable tmp2 = TaskOverseeDAL.GetLastRemark(prjIDs);

                foreach (DataRow dr in dt.Rows)
                {
                    IEnumerable<DataRow> rows = from row in tmp1.AsEnumerable()
                                                where (int)row["PrjID"] == (int)dr["ID"]
                                                select row;

                    if (rows.Count() == 1)
                    {
                        dr["ScheduleRemark"] = StrUtil.Truncate((string)rows.First()["Remark"],40,"...");
                        dr["ScheduleUserName"] = rows.First()["UserName"];
                        dr["ScheduleInputTime"] = rows.First()["InputTime"];
                    }

                    rows = from row in tmp2.AsEnumerable()
                           where (int)row["PrjID"] == (int)dr["ID"]
                           select row;

                    if (rows.Count() == 1)
                    {
                        dr["OverseeRemark"] = StrUtil.Truncate((string)rows.First()["Remark"], 40,"...");
                        dr["OverseeUserName"] = rows.First()["UserName"];
                        dr["OverseeInputTime"] = rows.First()["InputTime"];
                    }

                }

            }

            return dt;
        }

        public override ProjectInfo GetInfo(object id)
        {
            ProjectInfo info = base.GetInfo(id);
            if (id != null)
            {
                List<int> memberIDs = _prj.GetMemberIDs(id);
                for (int i = 0; i < memberIDs.Count; i++)
                {
                    memberIDs[i] = -memberIDs[i];
                }
                info.InnerMemberIDs = StrUtil.MergeToStr(memberIDs, ",");
            }
            return info;
        }

        public override void Save(ProjectInfo info)
        {
            base.Save(info);
            List<int> memberIDs = StrUtil.SplitToInt(info.InnerMemberIDs);
            for (int i = 0; i < memberIDs.Count; i++)
            {
                memberIDs[i] = -memberIDs[i];
            }
            _prj.SaveMemberIDs(info.ID, memberIDs);

            //给任务表的树加上根
            PrjTaskBO task = new PrjTaskBO(info.ID);


            PrjTaskInfo taskInfo = new PrjTaskInfo();
           
            taskInfo.Name = info.Name;
            taskInfo.PrjID = info.ID;
            taskInfo.StartTime = info.StartTime;
            taskInfo.EndTime = info.EndTime;
            taskInfo.IsMilestone = false;
            taskInfo.Orders = 0;
            taskInfo.Path = ",";
            taskInfo.UpperID = 0;
            taskInfo.Depth = 0;
            taskInfo.UserIDs = info.PMID.ToString();
            taskInfo.UserNames = info.PMName;
            taskInfo.CreatorID = info.CreatorID;
            taskInfo.CreatorName = info.CreatorName;

            task.Save(taskInfo);
            
        }

        public DataTable GetPrjMembers(string prjID)
        {
            DataTable dt = _prj.GetPrjMembers(prjID);
            return dt;
        }

        public List<int> GetAllTaskID(string prjID)
        {
            return _prj.GetAllTaskID(prjID);
        }

        public void SetStatus(int id, PrjStatus status)
        {
            _prj.SetStatus(id, status);
        }

        public void SetStatus(List<int> ids, PrjStatus status)
        {
            _prj.SetStatus(ids, status);
        }

       
    }
}
